package com.vkdasari.youtube.ui.model;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

import com.vkdasari.youtube.model.Video;

public class VideoUploadTableModel extends AbstractTableModel {

	final String[] columnNames = {"Id", "File Name", "Size",
			"Location","Status"};
	

	private Class columnClasses[] = { String.class, String.class, String.class,
			String.class, String.class };

	private ArrayList data = new ArrayList();

	@Override
	public int getColumnCount() {
		return columnNames.length;
	}
	
	public String getColumnName(int col) {
		return columnNames[col];
	}
	
	public Class getColumnClass(int c) {
		return columnClasses[c];
	}

	@Override
	public int getRowCount() {
		return data.size();
	}
	
	public boolean isCellEditable(int r, int c) {
		return false;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return ( (Object[])data.get(rowIndex))[columnIndex];
	}
	
	public void setValueAt(Object value, int row, int col) {
		((Object[])data.get(row))[col] = value;
		fireTableCellUpdated(row, col);
	}
	
	
	/**
	 * Adds an EFile Object as a new row
	 * 
	 * @param efile EFile
	 */	
	public void addRow(Video Video){
		Object[] obj = {Video.getId(), Video.getName(), Video.getSize(), Video.getFilelocation(), Video.getStatus()};
		data.add(obj);
		int size = getRowCount();
		fireTableRowsInserted(size-1,size-1);
	}
    
	public void removeRow(int rowNo){
		Video Video = getVideo(rowNo);
		data.remove(rowNo);
		fireTableRowsDeleted(rowNo,rowNo);
	}
	
	
	public void removeRows(int[] rows){
		for(int rowNum : rows){
			data.remove(rowNum);
		}
		for(int rowNum : rows){
			fireTableRowsDeleted(rowNum,rowNum);
		}
	}
	
	/**
	 * Gets the Video Object at a particular row
	 * 
	 * @param rowNo row number
	 * @return Video Video Object
	 */
	public Video getVideo(int rowNo){
		Video hFile = new Video();
		Object[] ary = (Object[])data.get(rowNo);
		hFile.setId(ary[0].toString());
		hFile.setName(ary[1].toString());
		hFile.setSize(ary[2].toString());
		hFile.setFilelocation(ary[3].toString());
		hFile.setStatus(ary[4].toString());
		
		return hFile;
	}
	
	/**
	 * Gets the Video objects stored in this model
	 * 
	 * @return Array of Video Objects.
	 */
	public Video[] getVideos(){
		Video[] VideoAry = new Video[data.size()];
		for(int i=0; i<data.size(); i++){
			VideoAry[i] = getVideo(i);
		}
		return VideoAry;
	}
	
	

}
